<template>
	<view class="content">
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> &nbsp;&nbsp; 车牌号：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item">{{ baseFormData.plateNo }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 预约类型：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item"> {{ baseFormData.orderType==1?"洗车":"维修" }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> &nbsp;&nbsp; 预约人：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item"> {{ baseFormData.contactName }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 联系方式：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item">{{ baseFormData.mobile }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 到店时间：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item">{{ baseFormData.arrivalTime }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 取车时间：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item"> {{ baseFormData.pickTime }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 取车地址：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item">{{ baseFormData.pickAddress }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 送车时间：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item"> {{ baseFormData.deliveryTime }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 交车地址：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item"> {{ baseFormData.deliveryAddress }}</view>
			</uni-col>
		</uni-row>
		<uni-row class="list">
			<uni-col :span="8">
				<view class="item"> 备注：</view>
			</uni-col>
			<uni-col :span="16">
				<view class="item"> {{ baseFormData.remark }}</view>
			</uni-col>
		</uni-row>
		<view style="margin-top:100rpx;">
			<uni-row :gutter="20" v-if="isAdmin == 2">
				<uni-col :span="8">
					<u-button type="primary" v-if="baseFormData.pickAddress && baseFormData.status == 0" :plain="true"
						@click="pickCar">取 车</u-button>
				</uni-col>
				<uni-col :span="8">
					<u-button type="primary" v-if="baseFormData.deliveryAddress && baseFormData.status == 2" :plain="true"
						@click="deliveryCar">送 车</u-button>
				</uni-col>
				<uni-col :span="8">
					<u-button type="primary" v-if="baseFormData.status == 0" @click="confirm(1)"
						:plain="true">开 工</u-button>
				</uni-col>
			</uni-row>
		</view>
	</view>
</template>

<script>
	import {
		domain,
		appointmentReminder,
		storeInfo
	} from '@/utils/config.js';
	import dayjs from 'dayjs';
	export default {
		data() {
			return {
				baseFormData: {
					orderType: 1,
					contactName: '',
					plateNo: "",
					mobile: uni.getStorageSync('ownerMobile'),
					pickTime: null,
					arrivalTime: "",
					pickAddress: '',
					deliveryTime: null,
					deliveryAddress: "",
					remark: "",
					source: "",
					deliveryLatitude: "",
					deliveryLongitude: '',
					pickLatitude: '',
					pickLongitude: ''
				},
				sexs: [{
					text: '洗车',
					value: 1
				}, {
					text: '维修',
					value: 2
				}],
				// 校验规则
				rules: {
					contactName: {
						rules: [{
							required: true,
							errorMessage: '预约人不能为空'
						}]
					},
					plateNo: {
						rules: [{
							required: true,
							errorMessage: '车牌号不能为空'
						}]
					},
					mobile: {
						rules: [{
							required: true,
							errorMessage: '联系方式不能为空'
						}]
					},
					arrivalTime: {
						rules: [{
							required: true,
							errorMessage: '到店时间不能为空'
						}]
					}
				},
				openid: uni.getStorageSync('openid'),
				tmplIds: 'SnbotvI9eVttWNM08K5MSpqUPXZQgbYOyPk01VC5Ku8',
				isAdmin: uni.getStorageSync('isAdmin'),
			}
		},
		onLoad(options) {
			// const encodedParam = options.param;
			this.init(options);
		},
		methods: {
			init(options) {
				uni.request({
					url:  "reserve/queryOrder",
					method: "POST",
					data: {
						id: options.id
					},
					success: (res) => {
						console.log(res.data.data);
						this.baseFormData = {
							...res.data.data
						}
					}
				})
			},
			confirm(status) {
				uni.request({
					url: "reserve/updateApp",
					method: "POST",
					data: {
						...this.baseFormData,
						status,
					},
					success: (res) => {
						this.init(this.baseFormData);
						uni.showToast({
							title: '保存成功',
							duration: 2000,
						});
					}
				})
			},
			pickCar() {
				uni.openLocation({
					type: 'gcj02',
					name: this.baseFormData.pickAddress,
					address: this.baseFormData.pickAddress,
					latitude: this.baseFormData.pickLatitude * 1,
					longitude: this.baseFormData.pickLongitude * 1,
					success: function(res) {
						console.log(res)
					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			deliveryCar() {
				uni.openLocation({
					type: 'gcj02',
					name: this.baseFormData.deliveryAddress,
					address: this.baseFormData.pickAddress,
					latitude: this.baseFormData.deliveryLatitude * 1,
					longitude: this.baseFormData.deliveryLongitude * 1,
					success: function(res) {
						console.log(res)
					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			daohang() {
				uni.openLocation({
					...storeInfo,
					success: function(res) {
						console.log(res)
					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			chooseAddress(type = 1) {
				console.log('表单数据信息：');
				let that = this;
				uni.getSetting({
					success(res) {
						console.log(res);
						if (!res.authSetting['scope.userLocation']) {
							console.log("f1")
							uni.authorize({
								scope: 'scope.userLocation',
								success() {
									uni.chooseLocation({
										success: function(res) {
											if (type == 1) {
												that.baseFormData.pickAddress = res.address;
												that.baseFormData.pickLatitude = res.latitude;
												that.baseFormData.pickLongitude = res
												.longitude;
											}
											if (type == 2) {
												console.log(res)
												that.baseFormData.deliveryAddress = res
												.address;
												that.baseFormData.deliveryLatitude = res
													.latitude;
												that.baseFormData.deliveryLongitude = res
													.longitude;
											}
										},
									})
									console.log('success2')
								},
								fail() {
									console.log("f2")
								}
							})
						} else {
							uni.chooseLocation({
								success: function(res) {
									console.log(res)
									if (type == 1) {
										that.baseFormData.pickAddress = res.address;
										that.baseFormData.pickLatitude = res.latitude;
										that.baseFormData.pickLongitude = res.longitude;
									}
									if (type == 2) {
										console.log(res)
										that.baseFormData.deliveryAddress = res.address;
										that.baseFormData.deliveryLatitude = res.latitude;
										that.baseFormData.deliveryLongitude = res.longitude;
									}
								},
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.content {
		padding: 0 30rpx;

		.item {
			font-size: 40rpx;
			line-height: 80rpx;
		}
	}
</style>